package com.zd.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zd.dao.OrderSettingDao;
import com.zd.pojo.OrderSetting;
import com.zd.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量导入预约设置数据
    @Override
    public void add(List<OrderSetting> list) {
        if(list!=null&&list.size()>0){
            for (OrderSetting orderSetting : list) {
                //判断当期日期是否已经进行了预约设置
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate>0){
                    //已经进行了预约设置，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //没有进行预约设置，执行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    //根据月份查询对用的预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {//格式：yyyy-MM
        String begin = date +"-1";//2022-3-1
        String end=date+"-31";//2022-3-31
        Map<String,String> map= new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //调用dao，根据日期范围查询预约设置数据
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        /*this.leftobj = [
        { date: 1, number: 120, reservations: 1 },
        { date: 3, number: 120, reservations: 1 },
        { date: 4, number: 120, reservations: 119 },
        { date: 6, number: 120, reservations: 1 },
        { date: 8, number: 120, reservations: 1 }
        ];*/
        List<Map> result=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(OrderSetting orderSetting:list){
                Map<String,Object> m =new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());//获取日期数字
                m.put("number",orderSetting.getNumber());//可预约人数
                m.put("reservations",orderSetting.getReservations());//已预约人数
                System.out.println(m.get("date"));
                System.out.println(m.get("number"));
                System.out.println(m.get("reservations"));
                result.add(m);
            }
        }
        return result;
    }

    //根据预约日期设置预约数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //这里需要判断这里是否进行了预约设置 因为可能有人预约几个月后的体检，但是那里还没有进行那个月的预约设置
        Date orderDate = orderSetting.getOrderDate();
        //根据日期查询是否已经进行了预约设置
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if(count>0){
            //当前日期已经进行了预约设置，需要执行更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，需要执行插入操作
            orderSettingDao.add(orderSetting);
        }

    }
}
